<?php

namespace app\suguan\utils;

use think\facade\Cache;

/**
 * 后台登录
 */
class User
{
    /**
     * 登录用户
     * @return string
     */
    public static function getUid()
    {
        $token = self::getToken();
        return Cache::get('suguan_admin_uid_'. $token);
    }

    public static function getToken()
    {
        return request()->header('X-Token') ? request()->header('X-Token') : request()->param('X-Token');
    }

    public static function removeToken()
    {
        $token = self::getToken();
        return Cache::rm('suguan_admin_uid_'. $token);
    }

    public static function login($username, $password, $user_type)
    {
        $result = model('Admin')->checkPassword($username, $password, $user_type);
        if ($result) {
            // 更新登录信息
            model('Admin')->login($result['uid']);
            $token = uniqid();
            Cache::set('suguan_admin_uid_'. $token, $result['uid']);
            return $token;
        }
        return $result;
    }
}
